package schedulo.core.task.provider;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import schedulo.core.task.ParseMessage;
import schedulo.core.task.Task;
import schedulo.core.task.provider.parameters.FileParameterSpec;
import schedulo.core.task.provider.parameters.ParameterSpec;
import schedulo.core.task.provider.parameters.ParametersException;

public class FileTaskProvider extends TaskProvider {

	private File filename;

	private LinkedList<Task> tasks;

	public static ParameterSpec[] getParametersSpec() {
		ParameterSpec[] params = new ParameterSpec[] { new FileParameterSpec(
				"filename") {
			public void setParameterValue(TaskProvider tp, File file) {
				((FileTaskProvider) tp).setFilename(file);
			}
		} };

		return params;
	}

	public FileTaskProvider(Spec providerSpec) throws ParametersException {
		super(providerSpec);
	}

	public void setFilename(File filename) {
		this.filename = filename;
	}

	// public String toString() {
	// ByteArrayOutputStream baos = new ByteArrayOutputStream();
	// try {
	// Task.save(tasks, baos);
	// } catch (IOException e) {
	//
	// }
	// return baos.toString();
	// }

	public static void main(String[] args) {
		ByteArrayInputStream bais = new ByteArrayInputStream(
				"-[100%]- : desc\n[50%] desc\n[13%] (3j) PL1/PL2\n[99%] (3-9j) PL: kjhkjhkjh\n[99] (3-9j) PL: kjhkjhkjh\n[55%] (3-9jk) PL: kjhkjhkjh\n[99%] (3-9j) PL1/PL2: kjhkjhkjh\n[13%] (?m) PL1/PL2\n-[27%]- (?.?j) PL1/PL2: PL1/PL2\n[34%] PL3\n"
						.getBytes());
		FileTaskProvider tl;

		LinkedList<Task> tasks = new LinkedList<Task>();

		try {
			Iterator<ParseMessage> itPM = Task.load(bais, tasks).iterator();
			while (itPM.hasNext()) {
				System.out.println(itPM.next());
			}

			// display the task file
			Task.save(tasks, System.out);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public List<Task> getTaskListImpl() throws IOException {
		if (tasks == null) {
			tasks = new LinkedList<Task>();
			FileInputStream fis = new FileInputStream(filename);
			Task.load(fis, tasks);
			fis.close();

			// attach tasks
			Iterator<Task> itT = tasks.iterator();
			while (itT.hasNext()) {
				itT.next().setTaskProvider(this);
			}
		}
		return tasks;
	}

	public void setTaskListImpl(List<Task> taskList) throws IOException {

		// detach tasks
		if (tasks != null) {
			Iterator<Task> itT = tasks.iterator();
			while (itT.hasNext()) {
				itT.next().setTaskProvider(null);
			}
		}

		tasks = new LinkedList<Task>(taskList);

		// attach tasks
		if (tasks != null) {
			Iterator<Task> itT = tasks.iterator();
			while (itT.hasNext()) {
				itT.next().setTaskProvider(this);
			}
		}

		save();
	}

	public void invalidate() {
		tasks = null;
	}

	public void save() throws IOException {
		FileOutputStream fos = new FileOutputStream(filename);
		Task.save(tasks, fos);
		fos.flush();
		fos.close();
		fireTaskProviderUpdated();
	}
}